﻿

CREATE PROCEDURE [dbo].[fares_FiltreazaRaportDocumente] 
    @filtruPart varchar(8000),
	@filtruArt varchar(8000),
	@filtruTipDoc varchar(8000),
	@data1 datetime,
	@data2 datetime,
	@DefalcatTVA bit,
	@receptie bit,
	@marfa bit,
	@CodUnitate smallint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  SELECT * FROM [fares_DocumenteDimensiuni] WHERE 
    (LEN(LTRIM(RTRIM(@filtruArt)))=0 OR (IDArticol IN (SELECT fart.value FROM dbo.SplitIntParameters(@filtruArt,',') fart))) AND
    (LEN(LTRIM(RTRIM(@filtruPart)))=0 OR (IDPartener IN (SELECT fart.value FROM dbo.SplitBigIntParameters(@filtruPart,',') fart))) AND
    (LEN(LTRIM(RTRIM(@filtruTipDoc)))=0 OR (IDTipDocument IN (SELECT fart.value FROM dbo.SplitBigIntParameters(@filtruTipDoc,',') fart))) AND
    (@receptie=0 OR (@receptie=1 AND [TipOperatie] LIKE '%I%' AND IDTipDocument=0)) AND
    ((@marfa=0 AND [ValoareTotalaCuAdaos]=0) OR (@marfa=1 AND [ValoareTotalaCuAdaos]<>0)) AND 
	CodUnitate = @CodUnitate AND [DataDoc]>=@data1 AND [DataDoc]<=@data2
	ORDER BY [DataDoc]
END